package com.shadow.study.java.base.util.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * json解析
 * <p>
 * 正则      -> 自己玩玩
 * fastJson -> ...
 * Gson     -> ...
 * Jackson  -> 好用
 */
@Slf4j
public class TestParseJSON {

    private static final int TEST_METHOD = 2;
    private static final String JSON_STRING = "{\"id\": \"1.01\",\"name\": \"jack\"}";
    private static final String JSON_ARRAY = "[{\"id\": \"1\",\"name\": \"jack\"},{\"id\": \"2\",\"name\": \"marry\"}]";

    @Data
    static class TestUser {
        String id;
        String name;
    }

    public static void main(String[] args) {
        switch (TEST_METHOD) {
            case 1:
                test1();
                break;
            case 2:
                byJson();
                break;
            case 3:
                byGson();
                break;
            case 4:
                byJackson();
                break;
            default:
                log.info("nothing");
        }
    }

    /**
     * 自己玩玩
     */
    private static void test1() {
        String a = JSON_STRING.replace(" ", "");
        String id = PatternUtils.getJSONValue(a, "id");
        String name = PatternUtils.getJSONValue(a, "name");
        log.info(id);
        log.info(name);

    }

    /**
     * JSON
     */
    private static void byJson() {
        JSON testUser = JSON.parseObject(JSON_STRING, JSON.class);
        String jsonString = JSON.toJSONString(testUser);
        log.info(jsonString);
        Map<String, Object> data = new HashMap<>();
        data.put("name", new BigDecimal("1.01"));
        String jsonString1 = JSON.toJSONString(testUser, filter, SerializerFeature.WriteNonStringKeyAsString,
                SerializerFeature.WriteNullStringAsEmpty);
        log.info(jsonString1);
        log.info(testUser.toString());
        List<TestUser> list = JSON.parseArray(JSON_ARRAY, TestUser.class);
        log.info(list.toString());
    }

    private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null) {
                return "";
            }
            return v;
        }
    };

    /**
     * Gson
     */
    private static void byGson() {
        Gson gson = new Gson();
        TestUser user = gson.fromJson(JSON_STRING, TestUser.class);
        log.info(user.toString());
    }

    /**
     * Jackson
     */
    private static void byJackson() {
        TestUser testUser;
        try {
            testUser = JacksonUtils.toObject(JSON_STRING, TestUser.class);
            log.info(testUser.toString());
            String jsonString = JacksonUtils.toJsonString(testUser);
            log.info(jsonString);
            List<TestUser> testUsers = JacksonUtils.toTypeObject(JSON_ARRAY, new TypeReference<List<TestUser>>() {
            });
            log.info(testUsers.toString());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
